package com.cainaer.shop.controller;

import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.utils.bean.BeanUtils;
import com.cainaer.common.core.web.controller.BaseController;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.shop.domain.ShopLogin;
import com.cainaer.shop.domain.vo.ShopVo;
import com.cainaer.shop.service.IShopLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商家登录 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-09-03
 */
@RestController
@RequestMapping("/")
public class ShopLoginController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ShopLoginController.class);

    @Autowired
    private IShopLoginService shopLoginService;

    /**
     * 商家登录
     * 如果用户信息不存在就新增
     *
     * @param login
     * @return
     */
    @PostMapping("/sign")
    AjaxResult shopLogin(@RequestBody ShopVo login) throws Exception {
        logger.info("login->{}", login);
        ShopLogin sign = shopLoginService.sign(login);
        if (!StringUtils.isNull(sign)) {
            BeanUtils.copyBeanProp(login, sign);
            return AjaxResult.success(login);
        }
        return AjaxResult.error();
    }

    /**
     * 判断商家是否存在
     *
     * @param shopId
     * @return
     */
    @GetMapping("isShopExist/{shopId}")
    public AjaxResult isShopExist(@PathVariable("shopId") Long shopId) {

        if (StringUtils.isNull(shopId)) {
            return AjaxResult.error("主键不能为空");
        }
        ShopLogin shopLogin = shopLoginService.selectShopById(shopId);
        if (StringUtils.isNull(shopLogin)) {
            return AjaxResult.error("无数据");
        }
        return AjaxResult.success(shopLogin);
    }

    /**
     * 编辑商家登录
     *
     * @param shopLogin
     * @return
     */
    @PostMapping("edit")
    public AjaxResult editShop(@RequestBody  ShopLogin shopLogin) {
        if (StringUtils.isNull(shopLogin.getShopId())) {
            return AjaxResult.error("主键不能为空");
        }
        return toAjax(shopLoginService.editShopLogin(shopLogin));
    }


}
